﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

namespace AsyncProgressNotify
{
	public class MyTaskProcessor
	{
		static readonly Random rnd = new Random();

		public void ProcessTasksAsync(IEnumerable<MyTask> tasks)
		{
			// process all tasks parallel in different threads
			new Task(
				() => {
					tasks
						.AsParallel()
						.WithDegreeOfParallelism(5)
						.ForAll(ProcessTasksInternal);
				}).Start();
		}

		private void ProcessTasksInternal(MyTask task)
		{
			// random complexicity (duration of this operation)
			int complexicity = rnd.Next(50, 200);

			for (int i = 1; i <= 10; i++)
			{
				// count progress
				double progress = ((double)i) / 10;

				// set dynamic property property with progress
				PropDef.dpMyTaskProgress.Set(task, progress);

				// simulate long operation
				Thread.Sleep(TimeSpan.FromMilliseconds(complexicity));
			}
		}
	}
}
